/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.notification.server.impl;

import java.util.ArrayList;
import java.util.Set;

import org.jb.notification.client.Notification;
import org.jb.notification.client.Notifications;
import org.jb.notification.client.rpc.NotificationService;
import org.jb.notification.server.NotificationManager;
import org.jb.notification.server.NotificationProvider;
import org.jb.room.server.RoomScoped;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;

@RoomScoped
public class NotificationServiceImpl implements NotificationService, NotificationManager {

	private static final Logger logger = LoggerFactory.getLogger(NotificationServiceImpl.class);
	private long timeout = 2 * 60 * 1000;
	
	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.notify.NotificationService#getNotification(long)
	 */
	public Notifications getNotification(long checkpoint) {
		
		long currentCheckpoint;
		
		synchronized( lock ) {
			
			if (checkpoint >= this.checkpoint) {	
				try {
					logger.debug("No notification available => locking [asked={} current={}]", checkpoint, this.checkpoint);
					lock.wait( timeout );
				} catch (InterruptedException e) {
					logger.debug("InterruptedException waiting for notification");
					throw new IllegalArgumentException("timeout");
				}
			}
			
			currentCheckpoint = this.checkpoint;
		}
		
		Notifications n = new Notifications();
		n.setCheckpoint( currentCheckpoint );
		ArrayList<Notification> notifications = new ArrayList<Notification>();
		n.setNotifications( notifications );	
		
		if (checkpoint >= currentCheckpoint) {
			logger.debug("No notification available - timeout => return to caller");
			return n;
		}
				
		for( NotificationProvider provider : providers ) {
			provider.notify(notifications, checkpoint, currentCheckpoint);
		}
				
		logger.debug("Sending notifications (from {} to {})", checkpoint, currentCheckpoint );
		
		return n;
	}

	/**
	 * 
	 * @param timeout
	 */
	public void setTimeout( long timeout ) {
		this.timeout = timeout;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.server.notify.NotificationManager#wakeup()
	 */
	public void wakeup() {
		synchronized( lock ) {
			lock.notifyAll();
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.server.notify.NotificationManager#getNextId()
	 */
	public long getNextId() {
		synchronized( lock ) {
			return ++checkpoint;
		}
	}
	
	@Inject
	public void setNotificationProvider( Set<NotificationProvider> providers ) {
		this.providers = providers;
	}
	
	private Set<NotificationProvider> providers;
	
	private Object lock = new Object();
	private long checkpoint = 0;

}
